package org.springblade.modules.repair.controller;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.img.Img;
import cn.hutool.core.img.ImgUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.freewayso.image.combiner.ImageCombiner;
import com.freewayso.image.combiner.element.ImageElement;
import com.freewayso.image.combiner.enums.Direction;
import com.freewayso.image.combiner.enums.OutputFormat;
import com.freewayso.image.combiner.enums.ZoomMode;
import com.google.common.collect.Lists;
import org.springblade.common.utils.RobotUtil;
import org.springblade.core.log.exception.ServiceException;
import org.springblade.core.oss.model.BladeFile;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tenant.mp.TenantEntity;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.ImageUtil;
import org.springblade.core.tool.utils.WebUtil;
import org.springblade.modules.repair.dto.RobotContentDTO;
import org.springblade.modules.repair.dto.RobotMessageDTO;
import org.springblade.modules.repair.entity.Robot;
import org.springblade.modules.repair.service.RobotService;
import org.springblade.modules.resource.builder.oss.OssBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;

import javax.imageio.stream.ImageOutputStream;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Arrays;
import java.util.List;

/**
 * @author Jackey
 * @date 2023年05月18日 14:17
 */
@RestController
@RequestMapping("repair/robot")
public class WeixinRobotController {

	@Autowired
	private RobotUtil robotUtil;
	@Autowired
	RobotService robotService;

	/**
	 * 获取机器人推送状态
	 */
	@GetMapping("/status/{elevatorId}")
	R<Boolean> getRobotStatus(@PathVariable Long elevatorId) {
		if (elevatorId == null) {
			return R.fail("电梯id不能为空");
		}
		return R.data(robotUtil.getRobotStatus(elevatorId));
	}

	/**
	 * 机器人推送微信群消息
	 */
	@PostMapping("/push/{elevatorId}")
	R<Boolean> pushMessage(@PathVariable Long elevatorId, @RequestBody RobotMessageDTO messageDTO) {
		if (elevatorId == null) {
			return R.fail("电梯id不能为空");
		}
		Robot robot = robotService.getOne(new LambdaQueryWrapper<Robot>().eq(TenantEntity::getTenantId, AuthUtil.getTenantId()));
		if (robot == null) {
			throw new ServiceException("未配置机器人信息");
		}

		if (messageDTO.getType() == 0) {
			robotUtil.sendRobotTextMessage(AuthUtil.getTenantId(),elevatorId, messageDTO);
			robotUtil.sendRobotCardMessage(AuthUtil.getTenantId(),elevatorId, messageDTO);
		} else if (messageDTO.getType() == 1) {
			robotUtil.sendRobotTextMessage(AuthUtil.getTenantId(),elevatorId, messageDTO);

		} else {
			robotUtil.sendRobotCardMessage(AuthUtil.getTenantId(),elevatorId, messageDTO);
		}

		return R.status(true);
	}


	/**
	 * 机器人推送微信群消息(保养,维修,救援)
	 * @param planId 工单id
	 * @param elevatorId 电梯id
	 * @param robotContentDTO 机器人内容
	 */
	@PostMapping("/sendRobotMessage/{planId}/{elevatorId}")
	public void sendRobotMessage(@PathVariable Long planId,@PathVariable Long elevatorId, @RequestBody RobotContentDTO robotContentDTO) {

		robotUtil.sendRobotMessage(RequestContextHolder.getRequestAttributes(),elevatorId,planId, robotContentDTO);

	}

	@GetMapping("/sendRobotMessage1")
	public void sendRobotMessage() {
		RobotContentDTO robotContentDTO = new RobotContentDTO();
		robotContentDTO.setMaintainType(1);
		robotContentDTO.setMaintainSubTypeDesc("【开始保养通知】");
		robotContentDTO.setLocationDesc("金色年华花园01-11栋左梯005");
		robotContentDTO.setMaintainStatusDesc("进行中");
		robotContentDTO.setElevatorStatusDesc("停机");
		robotContentDTO.setMaintainPersonDesc("徐浩远,徐霖");
		robotContentDTO.setMaintainPersonImgDesc("https://592800-erised-fresh.oss-cn-shenzhen.aliyuncs.com/upload/20230822/5111143101ea149aef29a99a16b5f0b3.jpg,https://thirdwx.qlogo.cn/mmopen/vi_32/POgEwh4mIHO4nibH0KlMECNjjGxQUq24ZEaGT4poC6icRiccVGKSyXwibcPq4BWmiaIGuG1icwxaQX6grC9VemZoJ8rg/132");
		robotContentDTO.setMaintainImgUrl("https://592800-erised-fresh.oss-cn-shenzhen.aliyuncs.com/upload/20230825/1ad67d492e9c1831cf5a3be9296541a3.jpg");
		robotContentDTO.setMaintainTimeDesc("2023-8-25 (今天) 14:42");
		robotContentDTO.setMalfunctionDesc("呵呵");

//		robotUtil.sendRobotMessage(1000000000000000093L,1674338534345740290L, robotContentDTO);

	}


}
